package com.relytosoft.mizar.analyze.system.services.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.mizar.analyze.system.api.entity.AyPersonGroup;
import com.relytosoft.mizar.analyze.system.services.mapper.AyPersonGroupMapper;
import com.relytosoft.mizar.analyze.system.services.service.AyPersonGroupService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lj
 * @date 2022/5/12 11:45
 */
@Service
public class AyPersonGroupServiceImpl extends ServiceImpl<AyPersonGroupMapper, AyPersonGroup> implements AyPersonGroupService {
    @Override
    public IPage<AyPersonGroup> selectAyPersonGroupPageList(PageQuery pageQuery, AyPersonGroup ayPersonGroup) {
        return baseMapper.selectPageGroupList(pageQuery.build(), ayPersonGroup);
    }

    @Override
    public List<AyPersonGroup> selectAyPersonGroupList(AyPersonGroup ayPersonGroup) {
        return baseMapper.selectGroupList(ayPersonGroup);
    }

    @Override
    public List<AyPersonGroup> selectGroupAndFace(List<Integer> personIds) {
        return this.baseMapper.selectGroupAndFace(personIds);
    }

    @Override
    public List<AyPersonGroup> selectAyPersonGroup(Set<Integer> groupIds, Integer faceId) {
        LambdaQueryWrapper<AyPersonGroup> wrapper = new QueryWrapper<AyPersonGroup>().lambda();
        wrapper
                .eq(AyPersonGroup::getPersonId, faceId)
                .in(AyPersonGroup::getGroupId, groupIds);
        return this.list(wrapper);
    }

    @Override
    public AyPersonGroup selectByPersonAndGroup(Integer personId, Integer groupId) {
        LambdaQueryWrapper<AyPersonGroup> wrapper = new QueryWrapper<AyPersonGroup>().lambda();
        wrapper
                .eq(AyPersonGroup::getPersonId, personId)
                .eq(AyPersonGroup::getGroupId, groupId);
        return this.getOne(wrapper);
    }

    @Override
    public List<Integer> selectByPersonId(Integer personId) {
        LambdaQueryWrapper<AyPersonGroup> wrapper = new QueryWrapper<AyPersonGroup>().lambda();
        wrapper.eq(AyPersonGroup::getPersonId, personId);
        List<AyPersonGroup> ayPersonGroupList = this.list(wrapper);
        return ayPersonGroupList.stream().map(AyPersonGroup::getGroupId).collect(Collectors.toList());
    }

    @Override
    public boolean removeByPersonIdAndGroupId(Integer personId, List<Integer> list) {
        if (list.size() > 0 && personId != null) {
            LambdaQueryWrapper<AyPersonGroup> wrapper = new QueryWrapper<AyPersonGroup>().lambda();
            wrapper
                    .eq(AyPersonGroup::getPersonId, personId)
                    .in(AyPersonGroup::getGroupId, list);
            return this.remove(wrapper);
        } else {
            return false;
        }
    }
}
